{% extends 'layout.html' %}
{% block main %}
    <script type=text/javascript>
        $(function() {
            function fillparentres(ress,selid){
                var optionstr='<option value="">无</option>';
                var selectoption='';
                $.each(ress,function(i,item){
                    if(selid!==''&&selid==item.ResID){
                        selectoption='selected="selected"';
                    }else{
                        selectoption='';
                    }
                    optionstr+='<option value="'+item.ResID+'"'+selectoption+'>'+item.InstallAddress+' --- '+item.DeviceName+'</option>';
                });
                $("select[name='parentres']").html(optionstr);
            }

            function filltbress(ress){
                var tr='';
                $.each(ress,function(i,item){
                    tr+='<tr><td>'+(i+1)+'</td><td>'+item.ResID+'</td><td><a target="_blank" href="/res/'+item.ResID+'">'+item.InstallAddress+'</a></td><td>'+item.DeviceName+'</td><td>'+item.RoomType+'</td><td>'+item.AssetNumber+'</td><td>'+item.SN+'</td><td>'+item.MAC+'</td><td>'+item.pInstallAddress+' - '+item.pDeviceName+'</td><td>'+item.Memo+'</td></tr>';

                });
                $("#tb_ress tbody").html(tr);
            }

            $("select[name='comm']").change(function(){
                $.getJSON("/_json_parentress",{commid:$("select[name='comm']").val()},function(data){
                    fillparentres(data.result,$("input[name='parentresid']").val());
                    filltbress(data.result);
                });

            });
            $("select[name='comm']").trigger('change');
            $("#form1").submit(function(){
                var device=$("select[name='device']").val();
                var parentres=$("select[name='parentres']").val();
                var osport=$("select[name='osport']").val();
                var clicked_button = $("input[type=submit][clicked=true]").val();
                console.log(clicked_button);
                if(clicked_button!="删除"){
                    if((device==11||device==30)&&parentres==''){
                        alert('请选择上级资源');
                        return false;
                    }
                    if((device==11||device==30)&&osport==''){
                        alert('请选择光站端口');
                        return false;
                    }
                }
            });
            $("form input[type=submit]").click(function() {
                $("input[type=submit]", $(this).parents("form")).removeAttr("clicked");
                $(this).attr("clicked", "true");
            });
        });
    </script>

        <form id="form1" class="ym-form ym-columnar" method="post">
            <div class="ym-grid">
                <div class="ym-g50 ym-gl">
                    <div class="ym-gbox ym-fbox-select">
                        <label for="comm">小区</label>
                        <select name="comm">
                            {% for comm in comms %}
                            <option value="{{ comm.CommID }}" {% if comm.CommID==res.Community %}selected="selected" {% endif %}>{{ comm.CommName }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="installaddress">安装地址</label>
                        <input name="installaddress" value="{{ res.InstallAddress or '' }}" />
                    </div>
                    <div class="ym-gbox ym-fbox-select">
                        <label for="device">资源类型</label>
                        <select name="device">
                            {% if res.Device==11 %}
                                <option value="11">分支器</option>
                            {% else %}
                                {% for device in devices %}
                                    <option value="{{ device.DevcID }}" {% if device.DevcID==res.Device %}selected="selected" {% endif %}>{{ device.DeviceType }} --- {{ device.Device }}</option>
                                {% endfor %}
                            {% endif %}
                        </select>

                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="assetnumber">资产编号</label>
                        <input name="assetnumber" value="{{ res.AssetNumber or '' }}" {% if res.Device==11 %}disabled="disabled"{% endif %}/>
                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="mac">MAC</label>
                        <input name="mac" value="{{ res.MAC or '' }}" {% if res.Device==11 %}disabled="disabled"{% endif %}/>
                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="memo">备注</label>
                        <textarea name="memo">{{ res.Memo or '' }}</textarea>
                    </div>

                </div>
                <div class="ym-g50 ym-gr">
                    <div class="ym-gbox ym-fbox-select">
                        <label for="parentres">上级资源</label>
                        <input type="hidden" name="parentresid" value="{{ res.ParentRes or '' }}" />
                        <select name="parentres">
{#                        <option value="">无</option>#}
{#                        {% for parentres in parentress %}#}
{#                            <option value="{{ parentres.ResID }}" {% if parentres.ResID==res.ParentRes %}selected="selected"{% endif %}>{{ parentres.InstallAddress }} --- {{ parentres.DeviceName }}</option>#}
{#                        {% endfor %}#}
                        </select>
                    </div>
                    <div class="ym-gbox ym-fbox-select">
                        <label for="osport">光站端口</label>
                        <select name="osport">
                            <option value="">无</option>
                            <option value="1" {% if res.OSPort=='1' %}selected="selected" {% endif %}>1</option>
                            <option value="2" {% if res.OSPort=='2' %}selected="selected" {% endif %}>2</option>
                            <option value="3" {% if res.OSPort=='3' %}selected="selected" {% endif %}>3</option>
                            <option value="4" {% if res.OSPort=='4' %}selected="selected" {% endif %}>4</option>
                        </select>

                    </div>
                    <div class="ym-gbox ym-fbox-select">
                        <label for="roomtype">资源箱体</label>
                        {% if res.Device!=11 %}
                        <select name="roomtype">
                            {% for roomtype in roomtypes %}
                                <option value="{{ roomtype.RoomTypeID }}" {% if roomtype.RoomTypeID==res.RoomTypeID %}selected="selected" {% endif %}>{{ roomtype.RoomType }}</option>
                            {% endfor %}
                        </select>
                        {% endif %}
                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="sn">SN</label>
                        <input name="sn" value="{{ res.SN or '' }}" {% if res.Device==11 %}disabled="disabled"{% endif %} />
                    </div>
                    <div class="ym-gbox ym-fbox-text">
                        <label for="resid">资源编号</label>
                        <input name="resid" value="{{ res.ResID or '' }}" disabled="disabled" />
                    </div>
                </div>
            </div>
            <div class="ym-fbox-button">
            {% if res.Device!=11 %}
                <input type="submit" name="action" value="新增" />
            {% endif %}
            {% if resid %}
                <input type="submit" class="ym-button" name="action" value="保存" />
                {% if res.Device==11 %}
                    <input type="submit" class="ym-button" name="action" value="删除" />
                {% endif %}
            {% endif %}
            </div>
        </form>
        <table id="tb_ress" class="bordertable">
            <thead>
            <tr>
                <th>序号</th>
                <th>资源编号</th>
                <th>安装地址</th>
                <th>资源类型</th>
                <th>资源箱体</th>
                <th>资产编号</th>
                <th>SN</th>
                <th>MAC</th>
                <th>上级资源</th>
{#                <th>上级资源地址</th>#}
                <th>备注</th>
            </tr>
            </thead>
            <tbody>
{#            {% for res in ress %}#}
{#            <tr>#}
{#                <td>{{ loop.index }}</td>#}
{#                <td>{{ res.InstallAddress }}</td>#}
{#                <td>{{ res.DeviceType }}-{{ res.DeviceName }}</td>#}
{#                <td>{{ res.AssetNumber }}</td>#}
{#                <td>{{ res.SN }}</td>#}
{#                <td>{{ res.MAC }}</td>#}
{#                <td>{{ res.ParentRes }}</td>#}
{#                <td>{{ res.Memo }}</td>#}
{#            </tr>#}
{#            {% endfor %}#}
            </tbody>
        </table>
{% endblock main %}